package com.example.springbootgraduation.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.springbootgraduation.entity.User;
import com.example.springbootgraduation.service.UserService;
import com.example.springbootgraduation.vo.DataView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//登录
@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    /**
     * 跳转login页面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }


    /**
     * 验证码逻辑
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("/login/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {//拿到response将图片输出出去，验证码拿到后要存到session里面去
//        1、拿到验证码对象（hutool）
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);//宽、高、密码位数、干扰条数
//        2、放入到session
        session.setAttribute("code",captcha.getCode());
//        3、输出验证码
        ServletOutputStream outputStream = response.getOutputStream();
        captcha.write(outputStream);//写出去
//        4、关闭输出流
        outputStream.close();
    }

    /**
     * 具体登录逻辑
     */
    @RequestMapping("/login/login")
    @ResponseBody
    public DataView login( String username,String password,String code,HttpSession session){//DataView数据返回格式，可以提示消息，到底有没有登录成功
        DataView dataView = new DataView();
//        1、首先判断验证码是否正确
        String sessionCode = (String) session.getAttribute("code");
        if (code!=null && sessionCode.equals(code)){
//            2、登录逻辑 session普通登录
//            User user=userService.login(username,password);
            /**
             * shiro登录
             */
            Subject subject = SecurityUtils.getSubject();
//            登录token
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            subject.login(token);//拿到token
            User user = (User) subject.getPrincipal();//强转成User对象，拿到用户
//            3、判断
            if (user!=null){
                dataView.setCode(200);
                dataView.setMsg("登录成功！");
                //4、放入session
                session.setAttribute("user",user);
                return dataView;
            }else{
                dataView.setCode(100);
                dataView.setMsg("用户名或密码有误，登录失败！");
                return dataView;
            }
        }
        dataView.setCode(100);
        dataView.setMsg("验证码错误！");
        return dataView;
    }

    /**
     * 退出登录
     */
    @RequestMapping("/login/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }
}
